/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.dao.impl;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pneuservis.backend.dao.OrderPneuDAOLocal;
import pneuservis.backend.entities.Customer;
import pneuservis.backend.entities.OrderPneu;
import pneuservis.backend.entities.ServicePneu;
import pneuservis.backend.entities.TyreType;

/**
 *
 * @author Admin
 */
@Stateless
public class OrderPneuDAO implements OrderPneuDAOLocal {

    @PersistenceContext(unitName="RunPU")
    private EntityManager entityManager;

    @Override
    public void createOrder(OrderPneu orderPneu) {
        validate(orderPneu);
        if (orderPneu.getId() != null) {
            throw new IllegalArgumentException("order id is set");
        }
        Customer customer = entityManager.find(Customer.class, orderPneu.getCustomer().getId());
        orderPneu.setCustomer(customer);
        TyreType tyreType = entityManager.find(TyreType.class, orderPneu.getTyreType().getId());
        orderPneu.setTyreType(tyreType);
        List<ServicePneu> merged = new ArrayList<>();
        for (ServicePneu servicePneu : orderPneu.getServicePneus()) {
            merged.add(entityManager.find(ServicePneu.class, servicePneu.getId()));
        }
        orderPneu.setServicePneus(merged);
        entityManager.persist(orderPneu);
    }

    @Override
    public OrderPneu getOrder(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id is null");
        }
        OrderPneu order = entityManager.find(OrderPneu.class, id);
        if (order == null) {
            return null;
        }
        entityManager.detach(order);
        return order;
    }

    @Override
    public void updateOrder(OrderPneu orderPneu) {
        validate(orderPneu);
        if (orderPneu.getId() == null) {
            throw new IllegalArgumentException("Cannot update order with null id");
        }
        OrderPneu orderFromDatabase = entityManager.find(OrderPneu.class, orderPneu.getId());

        if (orderFromDatabase == null) {
            throw new IllegalArgumentException("order is not in database" + orderPneu);
        }

        entityManager.merge(orderPneu);
        entityManager.flush();
    }

    @Override
    public void removeOrder(OrderPneu orderPneu) {
        if (orderPneu == null || orderPneu.getId() == null) {
            throw new IllegalArgumentException("orderPneu or id is null");
        }

        OrderPneu result = entityManager.find(OrderPneu.class, orderPneu.getId());

        if (result == null) {
            throw new IllegalArgumentException("order is not in databse");
        }

        entityManager.remove(result);

    }

    @Override
    public List<OrderPneu> getAllOrders() {

        List<OrderPneu> orders = entityManager.createQuery("SELECT o FROM OrderPneu o").getResultList();
        for (OrderPneu o : orders) {
            entityManager.detach(o);
        }
        return orders;
    }

    @Override
    public List<OrderPneu> getOrdersByCustomer(Customer customer) {
        if (customer == null || customer.getId() == null) {
            throw new IllegalArgumentException("customer or customer id is null");
        }

        Query query = entityManager.createQuery("SELECT o FROM OrderPneu o WHERE o.customer = :cid ");
        query.setParameter("cid", customer);
        List<OrderPneu> orders = query.getResultList();
        for (OrderPneu o : orders) {
            entityManager.detach(o);
        }
        return orders;
    }

    private void validate(OrderPneu order) {
        if (order == null) {
            throw new IllegalArgumentException("orderPneu is null");
        }
        if (order.getCustomer() == null) {
            throw new IllegalArgumentException("customer is null");
        }
        if (order.getCustomer().getId() == null) {
            throw new IllegalArgumentException("customer in order does not have id set");
        }
        if (order.getServicePneus() == null) {
            throw new IllegalArgumentException("list servicePneus is null, if no services are ordered, should be empty List");
        }
        if (order.getStatus() == null) {
            throw new IllegalArgumentException("status is null");
        }
        if (order.getVehicleBrand() == null) {
            throw new IllegalArgumentException("vehicle brand is null");
        }

    }
}
